package net.ipinglun.presenters;

import android.content.Context;

import com.loopj.android.http.AsyncHttpResponseHandler;

import net.ipinglun.http.GetNewsListResponseHandler;
import net.ipinglun.models.NewsBean;
import net.ipinglun.models.NewsListBean;
import net.ipinglun.http.HttpHelper;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class NewsPresenter {

    private Context mContext;
    private int mChannelId;


    public NewsPresenter(Context context) {
        mContext = context;
    }

    public void setChannelId(int channelId) {
        mChannelId = channelId;
    }

    public void getNewsList(int pager, final GetNewsListResponseHandler handler) {

        String url;
        if (mChannelId == 0) {
            url = "http://api.wallstreetcn.com/v2/posts?type=news&page=" + pager;
        } else {
            url = "http://api.wallstreetcn.com/v2/posts?cid=11&page=" + pager;
        }

        final NewsListBean list = new NewsListBean();

        // 从缓存加载
        //handler.onLoadCache(list);


        // 从网络加载
        HttpHelper.get(url, new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                handler.onStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                handler.onFinish();
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                  Throwable arg3) {

                handler.onFailure();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] content) {
                String jsonStr = new String(content);

                try {
                    String resStr = new JSONObject(jsonStr).getString("results");
                    JSONArray jsonArray = new JSONArray(resStr);


                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject obj = jsonArray.getJSONObject(i);
                        NewsBean news = new NewsBean();
                        news.id = obj.getString("id");
                        news.title = obj.getString("title");
                        news.image_url = obj.getString("imageUrl");
                        list.newsList.add(news);
                    }

                } catch (JSONException e) {
                    handler.onFailure();
                }

                handler.onSuccess(list);

            }

        });

    }


    /*
    public void getNewsList(int pager) {

        String url;
        if (mChannelId == 0) {
            url = "http://api.wallstreetcn.com/v2/posts?type=news&page=" + pager;
        } else {
            url = "http://api.wallstreetcn.com/v2/posts?cid=11&page=" + pager;
        }

        // 从网络加载
        final NewsListBean list = new NewsListBean();

        HttpHelper.get(url, new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                super.onStart();
                mDelegate.onLoadNewsListStart();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mDelegate.onLoadNewsListFinish();
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                  Throwable arg3) {

                mDelegate.onLoadNewsListFailure();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] content) {
                String jsonStr = new String(content);

                try {
                    String resStr = new JSONObject(jsonStr).getString("results");
                    JSONArray jsonArray = new JSONArray(resStr);

                    //System.out.println(resStr);

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject obj = jsonArray.getJSONObject(i);
                        NewsBean news = new NewsBean();
                        news.id = obj.getString("id");
                        news.title = obj.getString("title");
                        news.image_url = obj.getString("imageUrl");
                        list.newsList.add(news);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                mDelegate.onLoadNewsListSuccess(list);
            }

        });

    }*/
}
